<?php
/**
 * 
 * @author tangbl
 * 
 */
class CategoryAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
	
	/**
	 * 列表
	 */
	public function index(){
		$data  =  D("Category");
		if(IS_AJAX){
			$lang = $this->_get("lang","",getLang());
			$list = F('cate_'.$lang);
			
			$modi = L("cat_edit_page_title");
			$econ = L('cat_form_edit_cont');
			$acon = L('cat_form_add_cont');
			$add  = L('cat_form_add_child');
			foreach($list as $k => $r) {
				
				$uetid     = U('Category/edit/','id='.$r['id']);
				$r['edit'] = '<a href="'.$uetid.'" title="'.$modi.'" class="btn blue"><i class="icon-edit"></i></a>';
				
				$r['add']  = '<a href="'.U('Category/add/','id='.$r['id'].'&lang='.$r['lang']).'" class="btn green"><i class="icon-plus" title="'.$add.'"></i></a>';
				$module    = $r['module'] ;
				if($module == 'Artonce'){
					$r['cont'] = '<a href="'.U('Artonce/content/','id='.$r['id'].'&lang='.$r['lang']).'" class="btn blue"><i class="icon-signin" title="'.$econ.'"></i></a>';
				}else{
					if($module){
						$r['cont'] = '<a href="'.U($module.'/add/','cateid='.$r['id'].'&lang='.$r['lang']).'" class="btn green"><i class="icon-file" title="'.$acon.'"></i></a>';
					}else{
						$r['cont'] = '';
					}
				}
				
				$r['module']= $data->get_cate_module($r['module']);
				$r['isnav'] = ($r['isnav']==1) ? "Yes" : "No";
				
				if($r['haveLeaf'] == 1){
					$r['delt'] = '<a href="#" class="btn"  data-id="'.$r['id'].'"><i class="icon-remove"></i></a>';
				}else{
					$r['delt'] = '<a href="#" class="btn red delAlone"  data-id="'.$r['id'].'"><i class="icon-remove"></i></a>';
				}
				
				$r['link']   = empty($r['handurl']) ? $r['url'] : $r['handurl'] ;
				$r['levels'] = $r['level'];
				$array[]     = $r;
			}
			
			$str  = "<tr class='tr level_\$levels' leaf='\$haveLeaf' level='\$levels'>
				    <td align='center'><input style='width:55px' type='text' value='\$sort' size='3' name='sort[\$id]'></td>
				    <td class='name'>\$spacer \$title</td>
					<td class='center'>\$isnav</td>
				    <td align='center'>\$link</td>
				    <td align='center'>\$module</td>
					<td align='center'>
						\$add \$cont \$edit \$delt
					</td></tr>";
			Vendor('Common.Tree');
			$Tree = new Tree();
			$Tree->icon = array('　│　','　├─ ','　└─ ');
			$Tree->nbsp = '　';
			$Tree->init($array);
			$html_tree = $Tree->get_tree(0, $str);
			//echo $html_tree;
			$this->ajaxReturn(1,$html_tree,1);
		}else{
			$now_lang    = $this->_param("lang","",getLang());
			$lang_filter = C('LANG_ARRAY');
			foreach($lang_filter as $k=>$v){
				if($k==$now_lang){
					$array[$k] = array($v,'red');
				}else{
					$array[$k] = array($v,'');
				}
			}
			$this->assign("lang",$now_lang);
			$this->assign("lang_filter",$array);
			$this->display();
		}
		
	}
	
	/**
	 * 添加
	 */
	public function add(){
		
		$data  =  D("Category");
		if(IS_POST){
			if($data->create()){
				$solevar     = $data->solevar;
				$rec['lang'] = $data->lang;
				$rec['url']  = $data->url;
				$module      = $data->module;
				$rec['pid']  = $this->_param("pid","intval",0);
				$id   = $data->add();
				
				add_edit_thumb("single",$solevar,"Category");
				wlog(1,"",$id);//记录日志
				
				if($id){
					$row = $data->genCatePath($rec,$id);
					$data->data($row)->save();
					$data->clean_cate_cache($rec['lang'],$module);
				}
				$lang = $this->_param("formlang","trim",getLang());
				$this->assign("jumpUrl",U('Category/index','lang='.$lang));
				$this->success(L('cfg_form_tipmsg_addsucc'));
			}else{
				wlog(2,$data->getError());//记录日志
				$this->error($data->getError());
			}
		}else{
			
			$lanset = $this->_param("lang","",getLang());
			$this->assign("lang",$lanset);
			
			$array   = F("Module_cate");
			if(is_array($array)){
				foreach ($array as $k=>$v){
					$title = dejsonByLang($v[1]);
					$array[$k][1] = $title;
				}
				$modules = array_merge(C("LANG_CATEGORY"),$array);
			}else{
				$modules = C("LANG_CATEGORY");
			}
			$module  = create_option_array($modules,0,1,-1,true);
			$this->assign("module",$module);
			
			$fuarr  = F("Module_fu");
			foreach($fuarr as $k=>$v){
				$v['title'] = dejsonByLang($v['title'],$lanset);
				$row[] = $v ;
			}
			$this->assign("fuarr",ch_json_encode($row));
			
			$this->display();
		}
	}
	
	/**
	 * 修改
	 */
	public function edit(){
		$data  =  D("Category");
		$id    = $this->_param("id","intval",0);
		if(IS_POST){
			$old     = $data->find($id);
			if($data->create()){
				$solevar = $data->solevar;
				$res     = $data->save();
				$cate    = $data->find($id);
				
				//修改url
				if($old['module']!=$cate['module'] ){
					if($cate['module']){
						$save['url'] = $cate['url'].$id;
					}else{
						$save['url'] = "";
					}
					$save['id']  = $id;
					$data->save($save);
				}
				
				add_edit_thumb("single",$cate['solevar'],"Category");
				wlog(1,"",$id);
			
				$data->clean_cate_cache($cate['lang'],$cate['module']);
				
				$lang = $this->_param("formlang","trim",getLang());
				$this->assign("jumpUrl",U('Category/index','lang='.$lang));
				$this->success(L('cfg_form_tipmsg_editsucc'));
			}else{
				wlog(2,$data->getError());
				$this->error($data->getError());
			}
		}else{
			$cate    = $data->find($id);
			$this->assign("cate",$cate);
			
			$array   = F("Module_cate");
			if(is_array($array)){
				foreach ($array as $k=>$v){
					$title = dejsonByLang($v[1]);
					$array[$k][1] = $title;
				}
				$modules = array_merge(C("LANG_CATEGORY"),$array);
			}else{
				$modules = C("LANG_CATEGORY");
			}			
			$module  = create_option_array($modules,0,1,$cate['module'],true);
			$this->assign("module",$module);	
			
			$category  = D("Category");
			$selects   = $category->getAllCate($cate['lang'],$cate['pid']);
			$this->assign("selects",$selects);
			
			$single = find_thumb($cate['solevar'],'single',$module);
			$this->assign("single",$single);
			
			$fuarr  = F("Module_fu");
			foreach($fuarr as $k=>$v){
				$v['title'] = dejsonByLang($v['title'],$cate['lang']);
				$row[] = $v ;
			}
			$this->assign("fuarr",ch_json_encode($row)); /* */
			
			$this->display();
		}
	}
	
	/**
	 * 删除
	 */
	public function del(){
		 $data   = D("Category");
		 $id     = $this->_get('id',"intval","0");

		 $rec    = $data->find($id);
		 $res1   = $data->delete($id);
		 
		 $data->clean_cate_cache($rec['lang'],$rec['module']);
		 $data->updHaveLeaf($rec["pid"],true);
		 if($res1){
			wlog(1,"",$id);
			$this->ajaxReturn($res1,L('cfg_form_tipmsg_delsucc'),1);
		 }else{
			 wlog(2,"",$id);
			 $this->ajaxReturn(0,L('cfg_form_tipmsg_delfail'),0);
		 }
	}
	
	/**
	 * 栏目排序 
	 */
	public function sort(){
		$lang  = $this->_POST('lang');
		$sorts = $this->_POST('sort');
		
		if(!is_array($sorts))$this->error(L('cfg_form_tipmsg_illgel'));
		$model = D("Category");
		
		foreach ($sorts as $id => $sort) {
			$model->save(array("id"=>$id,"sort"=>$sort));
		}
		$model->clean_cate_cache($lang);
		$this->assign("jumpUrl",U('/Category/index','lang='.$lang));
		$this->success(L('cat_oth_sort_succ'));
	}
	
	/**
	 * 生成栏目缓存
	 */
	public function cache(){
		
		//重新生成栏目缓存
		$menu    = D("Category");
		$arr_lan = C('LANG_ARRAY');
		$arr_men = C('LANG_CATEGORY');
		foreach($arr_lan as $k=>$v){
			F("cate_".$k,NULL);
			$menu->getCateByModule($k);
			foreach($arr_men as $a=>$b){
				F("cate_".$k.$a,NULL);
				$menu->getCateByModule($k,$a);
			}
		}
		wlog(1);
		$this->success("生成栏目缓存成功");
	}
	
	/**
	 * 自定义菜单
	 */
	public function menu(){
		$where  = "varname = 'cfg_Category'";
		$syscfg = D("Sysconfig");
		$rows   = $syscfg->where($where)->find();
		if(IS_POST){
			$sub  = $_POST["subject"];
			$cmd  = $_POST["cmd"];
			$typ  = $_POST["type"];
			$type = 'click';
			$json = array();
			for($i=0;$i<=2;$i++){
				$json[$i]['name'] = $sub['up'][$i]['name'];
				$subchilds = $sub['sub'][$i];
				$cmdchilds = $cmd['sub'][$i];
				$typchilds = $typ['sub'][$i];
				$j=0;
				foreach($subchilds as $k=>$c){
					$name = $subchilds[$k]['name'];
					if(empty($name)) continue ;
					$json[$i]['sub_button'][$j]['name'] = $subchilds[$k]['name'];
					$json[$i]['sub_button'][$j]['type'] = $typchilds[$k]['type'];
					$json[$i]['sub_button'][$j]['key']  = $cmdchilds[$k]['cmd'];
					$j++;
				}
				if(empty($json[$i]['sub_button'])){
					$json[$i]['key']  = $cmd['up'][$i]['cmd'];
					$json[$i]['type'] = $type;
				}
			}
			F("Wxmenu",$json);
			wlog(1,"");
				
			$this->assign("jumpUrl",U('Category/menu'));
			$this->success(L('mwt_form_dtis_succ'));
		}else{
			$json = F("Wxmenu");
			$this->assign("json",$json);
			$this->display();
		}
	}
}

?>